<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>vec2.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_math_vec2.js.html">vec2.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2007 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Defines a 2-element vector class that can be used for
<a name="line17"></a> * coordinate math, useful for animation systems and point manipulation.
<a name="line18"></a> *
<a name="line19"></a> * Vec2 objects inherit from goog.math.Coordinate and may be used wherever a
<a name="line20"></a> * Coordinate is required. Where appropriate, Vec2 functions accept both Vec2
<a name="line21"></a> * and Coordinate objects as input.
<a name="line22"></a> *
<a name="line23"></a> * @author brenneman@google.com (Shawn Brenneman)
<a name="line24"></a> */
<a name="line25"></a>
<a name="line26"></a>goog.provide(&#39;goog.math.Vec2&#39;);
<a name="line27"></a>
<a name="line28"></a>goog.require(&#39;goog.math&#39;);
<a name="line29"></a>goog.require(&#39;goog.math.Coordinate&#39;);
<a name="line30"></a>
<a name="line31"></a>
<a name="line32"></a>
<a name="line33"></a>/**
<a name="line34"></a> * Class for a two-dimensional vector object and assorted functions useful for
<a name="line35"></a> * manipulating points.
<a name="line36"></a> *
<a name="line37"></a> * @param {number} x The x coordinate for the vector.
<a name="line38"></a> * @param {number} y The y coordinate for the vector.
<a name="line39"></a> * @constructor
<a name="line40"></a> * @extends {goog.math.Coordinate}
<a name="line41"></a> */
<a name="line42"></a>goog.math.Vec2 = function(x, y) {
<a name="line43"></a>  /**
<a name="line44"></a>   * X-value
<a name="line45"></a>   * @type {number}
<a name="line46"></a>   */
<a name="line47"></a>  this.x = x;
<a name="line48"></a>
<a name="line49"></a>  /**
<a name="line50"></a>   * Y-value
<a name="line51"></a>   * @type {number}
<a name="line52"></a>   */
<a name="line53"></a>  this.y = y;
<a name="line54"></a>};
<a name="line55"></a>goog.inherits(goog.math.Vec2, goog.math.Coordinate);
<a name="line56"></a>
<a name="line57"></a>
<a name="line58"></a>/**
<a name="line59"></a> * @return {!goog.math.Vec2} A random unit-length vector.
<a name="line60"></a> */
<a name="line61"></a>goog.math.Vec2.randomUnit = function() {
<a name="line62"></a>  var angle = Math.random() * Math.PI * 2;
<a name="line63"></a>  return new goog.math.Vec2(Math.cos(angle), Math.sin(angle));
<a name="line64"></a>};
<a name="line65"></a>
<a name="line66"></a>
<a name="line67"></a>/**
<a name="line68"></a> * @return {!goog.math.Vec2} A random vector inside the unit-disc.
<a name="line69"></a> */
<a name="line70"></a>goog.math.Vec2.random = function() {
<a name="line71"></a>  var mag = Math.sqrt(Math.random());
<a name="line72"></a>  var angle = Math.random() * Math.PI * 2;
<a name="line73"></a>
<a name="line74"></a>  return new goog.math.Vec2(Math.cos(angle) * mag, Math.sin(angle) * mag);
<a name="line75"></a>};
<a name="line76"></a>
<a name="line77"></a>
<a name="line78"></a>/**
<a name="line79"></a> * Returns a new Vec2 object from a given coordinate.
<a name="line80"></a> * @param {!goog.math.Coordinate} a The coordinate.
<a name="line81"></a> * @return {!goog.math.Vec2} A new vector object.
<a name="line82"></a> */
<a name="line83"></a>goog.math.Vec2.fromCoordinate = function(a) {
<a name="line84"></a>  return new goog.math.Vec2(a.x, a.y);
<a name="line85"></a>};
<a name="line86"></a>
<a name="line87"></a>
<a name="line88"></a>/**
<a name="line89"></a> * @return {!goog.math.Vec2} A new vector with the same coordinates as this one.
<a name="line90"></a> * @override
<a name="line91"></a> */
<a name="line92"></a>goog.math.Vec2.prototype.clone = function() {
<a name="line93"></a>  return new goog.math.Vec2(this.x, this.y);
<a name="line94"></a>};
<a name="line95"></a>
<a name="line96"></a>
<a name="line97"></a>/**
<a name="line98"></a> * Returns the magnitude of the vector measured from the origin.
<a name="line99"></a> * @return {number} The length of the vector.
<a name="line100"></a> */
<a name="line101"></a>goog.math.Vec2.prototype.magnitude = function() {
<a name="line102"></a>  return Math.sqrt(this.x * this.x + this.y * this.y);
<a name="line103"></a>};
<a name="line104"></a>
<a name="line105"></a>
<a name="line106"></a>/**
<a name="line107"></a> * Returns the squared magnitude of the vector measured from the origin.
<a name="line108"></a> * NOTE(brenneman): Leaving out the square root is not a significant
<a name="line109"></a> * optimization in JavaScript.
<a name="line110"></a> * @return {number} The length of the vector, squared.
<a name="line111"></a> */
<a name="line112"></a>goog.math.Vec2.prototype.squaredMagnitude = function() {
<a name="line113"></a>  return this.x * this.x + this.y * this.y;
<a name="line114"></a>};
<a name="line115"></a>
<a name="line116"></a>
<a name="line117"></a>/**
<a name="line118"></a> * @return {!goog.math.Vec2} This coordinate after scaling.
<a name="line119"></a> * @override
<a name="line120"></a> */
<a name="line121"></a>goog.math.Vec2.prototype.scale =
<a name="line122"></a>    /** @type {function(number, number=):!goog.math.Vec2} */
<a name="line123"></a>    (goog.math.Coordinate.prototype.scale);
<a name="line124"></a>
<a name="line125"></a>
<a name="line126"></a>/**
<a name="line127"></a> * Reverses the sign of the vector. Equivalent to scaling the vector by -1.
<a name="line128"></a> * @return {!goog.math.Vec2} The inverted vector.
<a name="line129"></a> */
<a name="line130"></a>goog.math.Vec2.prototype.invert = function() {
<a name="line131"></a>  this.x = -this.x;
<a name="line132"></a>  this.y = -this.y;
<a name="line133"></a>  return this;
<a name="line134"></a>};
<a name="line135"></a>
<a name="line136"></a>
<a name="line137"></a>/**
<a name="line138"></a> * Normalizes the current vector to have a magnitude of 1.
<a name="line139"></a> * @return {!goog.math.Vec2} The normalized vector.
<a name="line140"></a> */
<a name="line141"></a>goog.math.Vec2.prototype.normalize = function() {
<a name="line142"></a>  return this.scale(1 / this.magnitude());
<a name="line143"></a>};
<a name="line144"></a>
<a name="line145"></a>
<a name="line146"></a>/**
<a name="line147"></a> * Adds another vector to this vector in-place.
<a name="line148"></a> * @param {!goog.math.Coordinate} b The vector to add.
<a name="line149"></a> * @return {!goog.math.Vec2}  This vector with {@code b} added.
<a name="line150"></a> */
<a name="line151"></a>goog.math.Vec2.prototype.add = function(b) {
<a name="line152"></a>  this.x += b.x;
<a name="line153"></a>  this.y += b.y;
<a name="line154"></a>  return this;
<a name="line155"></a>};
<a name="line156"></a>
<a name="line157"></a>
<a name="line158"></a>/**
<a name="line159"></a> * Subtracts another vector from this vector in-place.
<a name="line160"></a> * @param {!goog.math.Coordinate} b The vector to subtract.
<a name="line161"></a> * @return {!goog.math.Vec2} This vector with {@code b} subtracted.
<a name="line162"></a> */
<a name="line163"></a>goog.math.Vec2.prototype.subtract = function(b) {
<a name="line164"></a>  this.x -= b.x;
<a name="line165"></a>  this.y -= b.y;
<a name="line166"></a>  return this;
<a name="line167"></a>};
<a name="line168"></a>
<a name="line169"></a>
<a name="line170"></a>/**
<a name="line171"></a> * Rotates this vector in-place by a given angle, specified in radians.
<a name="line172"></a> * @param {number} angle The angle, in radians.
<a name="line173"></a> * @return {!goog.math.Vec2} This vector rotated {@code angle} radians.
<a name="line174"></a> */
<a name="line175"></a>goog.math.Vec2.prototype.rotate = function(angle) {
<a name="line176"></a>  var cos = Math.cos(angle);
<a name="line177"></a>  var sin = Math.sin(angle);
<a name="line178"></a>  var newX = this.x * cos - this.y * sin;
<a name="line179"></a>  var newY = this.y * cos + this.x * sin;
<a name="line180"></a>  this.x = newX;
<a name="line181"></a>  this.y = newY;
<a name="line182"></a>  return this;
<a name="line183"></a>};
<a name="line184"></a>
<a name="line185"></a>
<a name="line186"></a>/**
<a name="line187"></a> * Rotates a vector by a given angle, specified in radians, relative to a given
<a name="line188"></a> * axis rotation point. The returned vector is a newly created instance - no
<a name="line189"></a> * in-place changes are done.
<a name="line190"></a> * @param {!goog.math.Vec2} v A vector.
<a name="line191"></a> * @param {!goog.math.Vec2} axisPoint The rotation axis point.
<a name="line192"></a> * @param {number} angle The angle, in radians.
<a name="line193"></a> * @return {!goog.math.Vec2} The rotated vector in a newly created instance.
<a name="line194"></a> */
<a name="line195"></a>goog.math.Vec2.rotateAroundPoint = function(v, axisPoint, angle) {
<a name="line196"></a>  var res = v.clone();
<a name="line197"></a>  return res.subtract(axisPoint).rotate(angle).add(axisPoint);
<a name="line198"></a>};
<a name="line199"></a>
<a name="line200"></a>
<a name="line201"></a>/**
<a name="line202"></a> * Compares this vector with another for equality.
<a name="line203"></a> * @param {!goog.math.Vec2} b The other vector.
<a name="line204"></a> * @return {boolean} Whether this vector has the same x and y as the given
<a name="line205"></a> *     vector.
<a name="line206"></a> */
<a name="line207"></a>goog.math.Vec2.prototype.equals = function(b) {
<a name="line208"></a>  return this == b || !!b &amp;&amp; this.x == b.x &amp;&amp; this.y == b.y;
<a name="line209"></a>};
<a name="line210"></a>
<a name="line211"></a>
<a name="line212"></a>/**
<a name="line213"></a> * Returns the distance between two vectors.
<a name="line214"></a> * @param {!goog.math.Coordinate} a The first vector.
<a name="line215"></a> * @param {!goog.math.Coordinate} b The second vector.
<a name="line216"></a> * @return {number} The distance.
<a name="line217"></a> */
<a name="line218"></a>goog.math.Vec2.distance = goog.math.Coordinate.distance;
<a name="line219"></a>
<a name="line220"></a>
<a name="line221"></a>/**
<a name="line222"></a> * Returns the squared distance between two vectors.
<a name="line223"></a> * @param {!goog.math.Coordinate} a The first vector.
<a name="line224"></a> * @param {!goog.math.Coordinate} b The second vector.
<a name="line225"></a> * @return {number} The squared distance.
<a name="line226"></a> */
<a name="line227"></a>goog.math.Vec2.squaredDistance = goog.math.Coordinate.squaredDistance;
<a name="line228"></a>
<a name="line229"></a>
<a name="line230"></a>/**
<a name="line231"></a> * Compares vectors for equality.
<a name="line232"></a> * @param {!goog.math.Coordinate} a The first vector.
<a name="line233"></a> * @param {!goog.math.Coordinate} b The second vector.
<a name="line234"></a> * @return {boolean} Whether the vectors have the same x and y coordinates.
<a name="line235"></a> */
<a name="line236"></a>goog.math.Vec2.equals = goog.math.Coordinate.equals;
<a name="line237"></a>
<a name="line238"></a>
<a name="line239"></a>/**
<a name="line240"></a> * Returns the sum of two vectors as a new Vec2.
<a name="line241"></a> * @param {!goog.math.Coordinate} a The first vector.
<a name="line242"></a> * @param {!goog.math.Coordinate} b The second vector.
<a name="line243"></a> * @return {!goog.math.Vec2} The sum vector.
<a name="line244"></a> */
<a name="line245"></a>goog.math.Vec2.sum = function(a, b) {
<a name="line246"></a>  return new goog.math.Vec2(a.x + b.x, a.y + b.y);
<a name="line247"></a>};
<a name="line248"></a>
<a name="line249"></a>
<a name="line250"></a>/**
<a name="line251"></a> * Returns the difference between two vectors as a new Vec2.
<a name="line252"></a> * @param {!goog.math.Coordinate} a The first vector.
<a name="line253"></a> * @param {!goog.math.Coordinate} b The second vector.
<a name="line254"></a> * @return {!goog.math.Vec2} The difference vector.
<a name="line255"></a> */
<a name="line256"></a>goog.math.Vec2.difference = function(a, b) {
<a name="line257"></a>  return new goog.math.Vec2(a.x - b.x, a.y - b.y);
<a name="line258"></a>};
<a name="line259"></a>
<a name="line260"></a>
<a name="line261"></a>/**
<a name="line262"></a> * Returns the dot-product of two vectors.
<a name="line263"></a> * @param {!goog.math.Coordinate} a The first vector.
<a name="line264"></a> * @param {!goog.math.Coordinate} b The second vector.
<a name="line265"></a> * @return {number} The dot-product of the two vectors.
<a name="line266"></a> */
<a name="line267"></a>goog.math.Vec2.dot = function(a, b) {
<a name="line268"></a>  return a.x * b.x + a.y * b.y;
<a name="line269"></a>};
<a name="line270"></a>
<a name="line271"></a>
<a name="line272"></a>/**
<a name="line273"></a> * Returns a new Vec2 that is the linear interpolant between vectors a and b at
<a name="line274"></a> * scale-value x.
<a name="line275"></a> * @param {!goog.math.Coordinate} a Vector a.
<a name="line276"></a> * @param {!goog.math.Coordinate} b Vector b.
<a name="line277"></a> * @param {number} x The proportion between a and b.
<a name="line278"></a> * @return {!goog.math.Vec2} The interpolated vector.
<a name="line279"></a> */
<a name="line280"></a>goog.math.Vec2.lerp = function(a, b, x) {
<a name="line281"></a>  return new goog.math.Vec2(goog.math.lerp(a.x, b.x, x),
<a name="line282"></a>                            goog.math.lerp(a.y, b.y, x));
<a name="line283"></a>};
</pre>


</body>
</html>
